﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using ASPNETMVC4Tutorial.Controllers;
using Rhino.Mocks;
using ASPNETMVC4Tutorial.Models;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using System.Web.Http;
using Moq;

namespace ASPNETMVC4Tutorial.Tests.Controllers
{

    public interface ISomethingUseful
    {
        int CalculateSomething(int x);
    }

    public class MyClass
    {
        public MyClass(ISomethingUseful useful)
        {
            this.useful = useful;
        }

        ISomethingUseful useful;

        public int MethodUnderTest(int x)
        {
            //Yah, it's dumb.
            return 1 + useful.CalculateSomething(x);
        }
    }


    [TestClass]
    public class ProductsControllerTest
    {
        /// <summary>
        /// This is the test method using mock framework named Moq.
        /// http://code.google.com/p/moq/wiki/QuickStart
        /// </summary>
        [TestMethod]
        public void TestProductGetPriceWithTaxUsingMoq()
        {
            //Initialize our product
            Product myProduct = new Product { Id = 1, Name = "Simple Product", Price = 25.0M };

            //Create a mock with Moq
            Mock<ITaxCalculator> fakeTaxCalculator = new Mock<ITaxCalculator>();

            // make sure to return 5$ of tax for a 25$ product
            fakeTaxCalculator.Setup(tax => tax.GetTax(25.0M)).Returns(5.0M);

            // Retrived the calculated tax
            decimal calculatedTax = myProduct.GetPriceWithTax(fakeTaxCalculator.Object);

            // Verify that the "GetTax" method was called from  the interface
            //fakeTaxCalculator.Verify(tax => tax.GetTax(25.0M));


            // Make sure that the taxes were calculated
            Assert.AreEqual(calculatedTax, 30.0M);
        }

        /// <summary>
        /// This is the test method using mock framework named Rhino.Mocks.
        /// 
        /// </summary>
        [TestMethod]
        public void TestProductGetPriceWithTaxUsingRhinoMocks()
        {
            //Mock the object.
            Rhino.Mocks.MockRepository mocks = new Rhino.Mocks.MockRepository();
            var mock = mocks.DynamicMock<ITaxCalculator>();
            SetupResult.For(mock.GetTax(25)).IgnoreArguments().Return(1);
            mocks.ReplayAll();

            //Initialize our product
            Product myProduct = new Product { Id = 1, Name = "Simple Product", Price = 25.0M };
            Assert.AreEqual(26, myProduct.GetPriceWithTax(mock));
        }



        [TestMethod]
        public void TestIndex()
        {

            

            Rhino.Mocks.MockRepository mocks = new Rhino.Mocks.MockRepository();
            IProductRepository mockProductRepository = mocks.StrictMock(typeof(ProductRepository)) as IProductRepository; 

            mockProductRepository.Add(new Product { Name = "Test Product", Category = "Groceries", Price = 1.39M });
            ApiProductsController control = new ApiProductsController(mockProductRepository);

            IEnumerable<Product> results = control.GetProducts();

            int resultcount = 0;

            foreach (Product item in results)
            {
                resultcount++;
            }

            Assert.AreEqual(resultcount, 3);

            control.DeleteProduct(3);
            results = control.GetProducts();
            int resultcount2 = 0;

            foreach (Product item in results)
            {
                resultcount2++;
            }

            Assert.AreEqual(resultcount2, 2);





        }
    }
}
